package com.weiyi.chat.presenter;

import com.google.gson.Gson;
import com.weiyi.chat.api.ILogin;
import com.weiyi.chat.common.MyStringCallBack;
import com.weiyi.chat.domain.User;
import com.weiyi.chat.ui.activity.LoginActivity;
import com.weiyi.chat.utils.MyReusouce;
import com.zhy.http.okhttp.OkHttpUtils;

import static com.weiyi.chat.utils.MyReusouce.VIRIFY_CODE_URL;

/**
 * Created by Lee on 2017/8/24 0024.
 */

public class LoginPresenter implements ILogin.presenter {


    private LoginActivity mView;

    public LoginPresenter(LoginActivity loginActivity) {
        mView = loginActivity;
    }

    @Override
    public void smsLogin(String user_phone, String sms) {
        OkHttpUtils.post()
                .url(MyReusouce.LOGIN_URL)
                .addParams("mobile", user_phone)
                .addParams("code", sms)
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        Gson gson = new Gson();
                        User user = gson.fromJson(response, User.class);
                        mView.onLoginSucess(user);
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });

    }

    @Override
    public void pswLogin(String account, String psw) {
        OkHttpUtils.post()
                .url(MyReusouce.LOGIN_URL)
                .addParams("account", account)
                .addParams("pass", psw)
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        Gson gson = new Gson();
                        User user = gson.fromJson(response, User.class);
                        mView.onLoginSucess(user);
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });

    }

    @Override
    public void requestSms(String phoneNum) {
        OkHttpUtils.get()
                .url(VIRIFY_CODE_URL)
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        mView.onRequestSmsSucess();
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void loadingDatasFromCache() {

    }
}
